package com.fbms.module.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.fbms.common.constants.Constants;
import com.fbms.module.common.ICheckFieldService;
import org.springframework.stereotype.Service;

/**
 * 类描述：校验字段是否重复
 *
 * @ClassName CheckFieldServiceImpl
 * @Author HappyBoy
 * @Date 2023-04-04 17:22
 **/
@Service
public class CheckFieldServiceImpl implements ICheckFieldService {

    /**
     * 校验字段是否重复
     * @param mapper mapper
     * @param func 字段
     * @param fieldValue 字段值
     * @return 0-否 1-是
     * @param <T> 类型
     */
    @Override
    public <T> String checkFieldUnique(BaseMapper<T> mapper, SFunction<T, ?> func, String fieldValue) {
        LambdaQueryWrapper<T> wrapper = new LambdaQueryWrapper<T>();
        wrapper.eq(func, fieldValue);
        Long integer = mapper.selectCount(wrapper);
        if(integer > 0){
            return Constants.NOT_UNIQUE;
        }
        return Constants.UNIQUE;
    }
}
